package com.qing.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qing.config.security.mapper.UsersMapper;
import com.qing.pojo.*;
import com.qing.mapper.EmployeeMapper;
import com.qing.pojo.vo.EmployeeInfVo;
import com.qing.pojo.vo.EmployeeSalaryVo;
import com.qing.service.DeptService;
import com.qing.service.EmployeeDetailsService;
import com.qing.service.EmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qing.service.JobService;
import com.qing.utils.EmployeeListener;
import com.qing.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author qinghua
 * @since 2022-06-06
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private EmployeeDetailsService employeeDetailsService;

    @Autowired
    private DeptService deptService;

    @Autowired
    private JobService jobService;

    @Autowired
    private FileUtil fileUtil;

    @Value("${file.uploadFolder}")
    private String uploadFolder;

    @Override
    public List<EmployeeSalaryVo> queryAllEmployeeInf() {
        return employeeMapper.queryAllEmployeeInf();
    }

    @Override
    public List<EmployeeSalaryVo> queryAllEmployeeSalary() {
        return employeeMapper.queryAllEmployeeSalary();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean deleteEmployee(Integer employeeId, Integer userId) {
        boolean b1 = usersMapper.deleteById(userId) == 1;
        boolean b2 = employeeDetailsService.remove(new QueryWrapper<EmployeeDetails>().eq("employee_id", employeeId));
        boolean b3 = removeById(employeeId);
        if (b1 == b2 == b3 == true){
            return true;
        }
        return false;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean updateInf(EmployeeInfVo employeeInfVo) {
//        System.out.println(employeeInfVo);
        // 更新员工表
        Employee employee = new Employee();
        employee.setId(employeeInfVo.getEmployeeId());
        employee.setBaseSalary(employeeInfVo.getBaseSalary());
        employee.setDeptId(employeeInfVo.getDeptId());
        employee.setJobId(employeeInfVo.getJobId());
        boolean b1 = updateById(employee);

        // 更新员工详细信息表
        EmployeeDetails employeeDetails = new EmployeeDetails();
        employeeDetails.setId(employeeInfVo.getId());
        employeeDetails.setEmployeeId(employeeInfVo.getEmployeeId());
        employeeDetails.setName(employeeInfVo.getName());
        employeeDetails.setSex(employeeInfVo.getSex());
        employeeDetails.setPhone(employeeInfVo.getPhone());
        employeeDetails.setCardNum(employeeInfVo.getCard_num());
        employeeDetails.setEducation(employeeInfVo.getEducation());
        employeeDetails.setAddress(employeeInfVo.getAddress());
        employeeDetails.setPostCode(employeeInfVo.getPost_code());
        employeeDetails.setTel(employeeInfVo.getTel());
        employeeDetails.setQqNum(employeeInfVo.getQq_num());
        employeeDetails.setEmail(employeeInfVo.getEmail());
        employeeDetails.setParty(employeeInfVo.getParty());
        employeeDetails.setBirthday(employeeInfVo.getBirthday());
        employeeDetails.setRace(employeeInfVo.getRace());
        employeeDetails.setSpeciality(employeeInfVo.getSpeciality());
        employeeDetails.setHobby(employeeInfVo.getHobby());
        employeeDetails.setRemake(employeeInfVo.getRemake());
        boolean b2 = employeeDetailsService.updateById(employeeDetails);

        if(b1==b2==true) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean insertByExcel(MultipartFile file) {
        Map<String, Integer> deptMap = new HashMap<String, Integer>();
        for (Dept dept : deptService.list()) {
            deptMap.put(dept.getName(), dept.getId());
        }

        Map<String, Integer> jobMap = new HashMap<String, Integer>();
        for (Job job : jobService.list()) {
            jobMap.put(job.getName(), job.getId());
        }

        // 有个很重要的点 DemoDataListener 不能被spring管理，要每次读取excel都要new,然后里面用到spring可以构造方法传进去
        String fileName = fileUtil.upload(file);
        fileName = uploadFolder + "/" + fileName.split("/")[2];
        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        try {
            EasyExcel.read(
                    fileName,
                    EmployeeExcel.class,
                    new EmployeeListener(usersMapper, employeeMapper, employeeDetailsService, deptMap, jobMap)
            ).sheet().doRead();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
